package com.beem.project.beem.ui.views;
import java.util.Iterator;
import java.util.List;

import com.beem.project.beem.R;
import com.beem.project.beem.service.Contact;
import com.butterfly.vv.Constants;
import com.butterfly.vv.db.City;
import com.butterfly.vv.db.Colleage;
import com.butterfly.vv.db.DBService;
import com.butterfly.vv.db.Provice;
import com.butterfly.vv.vv.utils.Debug;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
 
public class ParamsForFilterPopupWindow extends PopupWindow {
 
 
    private Button btn_take_photo, btn_pick_photo, btn_cancel,btn_defaut;
    TextView parameters_tx;
    private View mMenuView;
//    AutoCompleteTextView serverEdit;
    Activity mcontext;
    private Spinner prov_spr;
    private Spinner uni_spr;
   	private Spinner city_spr;
   	LinearLayout dialog_layout_content;
   	public String selectedPro,selectedCity,selectedUniName;
    List<Provice> provices;
    List<Colleage>colleages;
    DBService dbService;
    List<City> cities ;
    private String[] arrayOfString1 ;
    ArrayAdapter<String> adapter02;
    ArrayAdapter pro_adapter;
    ArrayAdapter uni_adapter;
    String selectedCityCode;
    LinearLayout layout_pro_city,layout_provice,layout_city,layout_university;
 
    public ParamsForFilterPopupWindow(Activity context,OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.params_for_filter, null);
//        bySex(itemsOnClick);
        setParams();
 
    }
    public ParamsForFilterPopupWindow(Activity context,OnClickListener itemsOnClick,int type) {
        super(context);
        this.mcontext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.params_for_filter, null);
        bySex(itemsOnClick,type);
        setParams();
 
    }
   
    
    public ParamsForFilterPopupWindow(Activity context,OnClickListener itemsOnClick,int type,int theme,
    		TextView text,DBService dbService,List<Provice> provices,ArrayAdapter<String> adapter02,ArrayAdapter pro_adapter) {
    	super(context);
    	this.mcontext = context;
    	this.dbService = dbService;
    	this.pro_adapter = pro_adapter;
    	this.adapter02 = adapter02;
    	this.provices = provices;
    	
    	LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.params_for_filter, null);
        bySex(itemsOnClick,type);
        setParams();
        }
    
    public ParamsForFilterPopupWindow(Activity context,OnClickListener itemsOnClick,int type,int theme,
    		TextView text,DBService dbService,List<Colleage> provices,ArrayAdapter pro_adapter) {
    	super(context);
    	this.mcontext = context;
    	this.dbService = dbService;
    	this.uni_adapter = pro_adapter;
    	this.colleages = provices;
    	
    	LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.params_for_filter, null);
        bySex(itemsOnClick,type);
        setParams();
        }

	private void bySex(OnClickListener itemsOnClick,int type) {
		btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
        btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        parameters_tx = (TextView) mMenuView.findViewById(R.id.parameters_tx);
        btn_defaut = (Button) mMenuView.findViewById(R.id.btn_default_cn);
        btn_defaut.setText("取消");
        dialog_layout_content =(LinearLayout) mMenuView.findViewById(R.id.dialog_layout_content);
        btn_defaut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
        if(type == Constants.SEX_POPUP){
        	parameters_tx.setText("按性别筛选");
        	dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setVisibility(View.VISIBLE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_cancel.setText("不限");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        if(type == Constants.SORT_POPUP){
        	parameters_tx.setText("排序");
	        btn_cancel.setVisibility(View.GONE);
	        dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setText("默认");
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_pick_photo.setText("按登陆时间排序");
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setText("按年龄排序");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        if(type == Constants.CITY_POPUP){
        	parameters_tx.setText("按地区、性别筛选");
        	dialog_layout_content.setVisibility(View.VISIBLE);
        	prov_spr = (Spinner) mMenuView.findViewById(R.id.provice_spiner01);
        	prov_spr.setAdapter(pro_adapter);
    		prov_spr.setOnItemSelectedListener(selectListener);
    		city_spr = (Spinner) mMenuView.findViewById(R.id.city_spiner02);
    		city_spr.setOnItemSelectedListener(selectListener01);
	        btn_cancel.setVisibility(View.VISIBLE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_cancel.setText("不限男女");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        if(type == Constants.COLLEAGUE_POPUP){
        	parameters_tx.setText("按学校、性别筛选");
        	dialog_layout_content.setVisibility(View.VISIBLE);
//        	layout_pro_city = (LinearLayout) mMenuView.findViewById(R.id.layout_pro_city);
//        	layout_pro_city.setVisibility(View.GONE);
//        	prov_spr = (Spinner) mMenuView.findViewById(R.id.provice_spiner01);
//        	prov_spr.setAdapter(pro_adapter);
//    		prov_spr.setOnItemSelectedListener(selectListener);
//    		city_spr = (Spinner) mMenuView.findViewById(R.id.city_spiner02);
//    		city_spr.setOnItemSelectedListener(selectListener01);
        	layout_provice = (LinearLayout) mMenuView.findViewById(R.id.layout_provice);
        	layout_provice.setVisibility(View.GONE);
        	layout_city = (LinearLayout) mMenuView.findViewById(R.id.layout_city);
        	layout_city.setVisibility(View.GONE);
        	layout_university = (LinearLayout) mMenuView.findViewById(R.id.layout_university);
        	layout_university.setVisibility(View.VISIBLE);
        	uni_spr = (Spinner) mMenuView.findViewById(R.id.spinnerUniv);
        	uni_spr.setAdapter(uni_adapter);
        	uni_spr.setOnItemSelectedListener(uniItemSelectedListener);
	        btn_cancel.setVisibility(View.VISIBLE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_cancel.setText("不限男女");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        if(type == Constants.ALBUM_MANAGE_POPUP){
        	parameters_tx.setText("相册编辑");
        	dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setVisibility(View.GONE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setText("编辑上传");
	        btn_pick_photo.setText("权限修改");
	        btn_cancel.setText("不限");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        
        if(type == Constants.ALBUM_IMAGE_CHOSE_POPUP){
        	parameters_tx.setText("相片来源");
        	dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setVisibility(View.GONE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setText("拍照");
	        btn_pick_photo.setText("从相册选择");
	        btn_cancel.setText("不限");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        
        if(type == Constants.ALBUM_IMAGE_PROCESS_POPUP){
        	parameters_tx.setText("照片设置");
        	dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setVisibility(View.GONE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setText("设为封面");
	        btn_pick_photo.setText("批量操作");
	        btn_cancel.setText("不限");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        
        if(type == Constants.ALBUM_IMAGE_AVARTA_POPUP){
        	parameters_tx.setText("头像设置");
        	dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setVisibility(View.GONE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setText("设为头像");
	        btn_pick_photo.setText("批量操作");
	        btn_pick_photo.setVisibility(View.GONE);
	        btn_cancel.setText("不限");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
        
        if(type == Constants.ALBUM_AUTH_POPUP){
        	parameters_tx.setText("更改相册权限");
        	dialog_layout_content.setVisibility(View.GONE);
	        btn_cancel.setVisibility(View.VISIBLE);
	        btn_pick_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setVisibility(View.VISIBLE);
	        btn_take_photo.setText("所有人可见");
	        btn_pick_photo.setText("好友可见");
	        btn_cancel.setText("指定好友");
	        btn_defaut.setVisibility(View.VISIBLE);
	        btn_defaut.setText("仅自己可见");
	        btn_cancel.setOnClickListener(itemsOnClick);
	        btn_pick_photo.setOnClickListener(itemsOnClick);
	        btn_take_photo.setOnClickListener(itemsOnClick);
        }
	}
	
	private OnItemSelectedListener uniItemSelectedListener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			selectedUniName = colleages.get(arg2).getUname();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	};
	
    private OnItemSelectedListener selectListener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			int pos = prov_spr.getSelectedItemPosition();
			cities = dbService.getAllCitys(provices.get(pos).getPcode());
			selectedPro = provices.get(pos).getPname();
			arrayOfString1 = new String[cities.size()];
			int j= 0;
			for (Iterator iterator = cities.iterator(); iterator.hasNext();) {
				City type = (City) iterator.next();
				Log.i("VV", "-=-="+type.getCname());
				arrayOfString1[j] = type.getCname();
				j++;
			}
			adapter02 = new ArrayAdapter<String>(mcontext,
					android.R.layout.simple_spinner_item, arrayOfString1);
			city_spr.setAdapter(adapter02);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	};

	private OnItemSelectedListener selectListener01 = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Debug.getDebugInstance().log(
					"-->" + city_spr.getSelectedItem().toString()+cities.get(arg2).getCcode()+cities.get(arg2).getCname());
			selectedCityCode = cities.get(arg2).getCcode();
			selectedCity = cities.get(arg2).getCname();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	};


	private void setParams() {
		//设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点�?
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.Popup_Animation_UpDown);
        //实例化一个ColorDrawable颜色为半透明
        //ColorDrawable dw = new ColorDrawable(0xb0000000);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背�?
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在�?择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {
             
            public boolean onTouch(View v, MotionEvent event) {
                 
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }               
                return true;
            }
        });
	}
 
}